package channel

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/query"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type WithdrawCollectionBankModel struct {
	ctx context.Context
	db  *gorm.DB
	logx.Logger
}

func NewWithdrawCollectionBankModel(ctx context.Context, db *gorm.DB) *WithdrawCollectionBankModel {
	return &WithdrawCollectionBankModel{
		ctx:    ctx,
		db:     db,
		Logger: logx.WithContext(ctx),
	}
}
func (model *WithdrawCollectionBankModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}

	return svc.Db
}

func (l *WithdrawCollectionBankModel) WithdrawCollectionBankListPage(in *rpc.GetWithDrawCollectionBankRequest) (*rpc.GetWithDrawCollectionBankResponse, error) {
	// todo: add your logic here and delete this line

	ret := &rpc.GetWithDrawCollectionBankResponse{}
	ret.ResHead = &rpc.Head{}
	g := query.Use(l.getDb()).TenantChannelBankAgentWithdraw
	h := g.Where()
	if in.Bankname == "" {
		if in.CollectionName != "" {
			h = h.Where(g.CollectionName.Eq(in.CollectionName))
		}
	} else {
		if in.CollectionName == "" {
			h = h.Where(g.BankName.Eq(in.Bankname))
		}
	}
	ret.ResHead.Count, _ = h.Count()
	if in.Page.Page < 1 {
		in.Page.Page = 1
	}
	if in.Page.PageSize < 0 {
		in.Page.PageSize = 10
	}
	AssetsBWithdrawCollectionBank, _, err := h.FindByPage(int((in.Page.Page-1)*in.Page.PageSize), int(in.Page.PageSize))
	if err != nil {
		return ret, err
	}
	for i, _ := range AssetsBWithdrawCollectionBank {
		ret.Data = append(ret.Data, &rpc.WithDrawCollectionBank{
			Id:             AssetsBWithdrawCollectionBank[i].ID,
			BankCode:       AssetsBWithdrawCollectionBank[i].BankCode,
			BankName:       AssetsBWithdrawCollectionBank[i].BankName,
			CollectionName: AssetsBWithdrawCollectionBank[i].CollectionName,
			Status:         AssetsBWithdrawCollectionBank[i].Status,
		})
	}
	return ret, nil
}
func (l *WithdrawCollectionBankModel) WithdrawCollectionBankUpdate(in *rpc.UpdateWithDrawCollectionBankRequest) (*rpc.UpdateWithDrawCollectionBankResponse, error) {
	// todo: add your logic here and delete this line

	ret := &rpc.UpdateWithDrawCollectionBankResponse{}
	Withdraw := &channel.TenantChannelBankAgentWithdraw{
		BankCode:       in.WithDrawCollectionBank.BankCode,
		BankName:       in.WithDrawCollectionBank.BankName,
		Status:         in.WithDrawCollectionBank.Status,
		CollectionName: in.WithDrawCollectionBank.CollectionName,
	}
	Withdraw.ID = in.WithDrawCollectionBank.Id
	g := query.Use(l.getDb()).TenantChannelBankAgentWithdraw
	g.Where(g.ID.Eq(in.WithDrawCollectionBank.Id)).Updates(Withdraw)
	ret.Data = &rpc.WithDrawCollectionBank{
		Id:             Withdraw.ID,
		BankCode:       Withdraw.BankCode,
		BankName:       Withdraw.BankName,
		CollectionName: Withdraw.CollectionName,
		Status:         Withdraw.Status,
	}
	return ret, nil
}
func (l *WithdrawCollectionBankModel) WithdrawCollectionBankAdd(in *rpc.BankAgentAddRequest) (*rpc.BankAgentAddResponse, error) {
	// todo: add your logic here and delete this line

	ret := &rpc.BankAgentAddResponse{}
	Withdraw := &channel.TenantChannelBankAgentWithdraw{
		BankCode:       in.BankAgent.BankCode,
		BankName:       in.BankAgent.BankName,
		Status:         in.BankAgent.Status,
		CollectionName: in.BankAgent.CollectionName,
	}
	d := l.getDb().Create(Withdraw)
	if d.Error != nil {
		return nil, xerrors.ErrBankAgentNotAdd
	}
	l.getDb().Last(Withdraw)
	ret.Data = &rpc.WithDrawCollectionBank{
		Id:             Withdraw.ID,
		BankCode:       Withdraw.BankCode,
		BankName:       Withdraw.BankName,
		CollectionName: Withdraw.CollectionName,
		Status:         Withdraw.Status,
	}
	return ret, nil
}
func (l *WithdrawCollectionBankModel) WithdrawCollectionBankDel(in *rpc.BankAgentDelRequest) (*rpc.BankAgentDelResponse, error) {
	// todo: add your logic here and delete this line

	ret := &rpc.BankAgentDelResponse{}
	g := query.Use(l.getDb()).TenantChannelBankAgentWithdraw
	g.Where(g.ID.Eq(in.Id)).Delete(&channel.TenantChannelBankAgentWithdraw{})
	return ret, nil
}
